package cn.danger.com.util;

import android.content.Context;
import android.os.Environment;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.bmob.v3.BmobUser;
import cn.danger.com.model.entity.MyUser;

/**
 * Created by 俊 on 2016/12/29.
 */
public class Check implements Constants {

    private Context context;

    public Check() {

    }

    public Check(Context context) {
        this.context = context;
    }

    public static String checkByCode(int code) {
        if (code == 101) {
            return USERNAME_OR_PASSWORD_IS_WRONG;
        }
        if (code == 202) {
            return USERNAME_IS_EXIST;
        }
        if (code == 203) {
            return EMAIL_IS_EXIST;
        }
        if (code == 206) {
            return ONLY_LOGIN_USER_CAN_MODIFY;
        }
        if (code == 207) {
            return CHECK_CODE_IS_WRONG;
        }
        if (code == 209) {
            return PHONE_IS_EXIST;
        }
        if (code == 210) {
            return ORIGIN_PASSWORD_IS_WRONG;
        }
        if (code == 304) {
            return LOGIN_DATA_IS_NULL;
        }
        if (code == 9001) {
            return APP_KEY_IS_NULL;
        }
        if (code == 9002) {
            return PARSE_DATA_ERROR;
        }
        if (code == 9003) {
            return UPLOAD_FILE_ERROR;
        }
        if (code == 9004) {
            return UPLOAD_FILE_FAILURE;
        }
        if (code == 9005) {
            return BATCH_OPERATION;
        }
        if (code == 9006) {
            return OBJECT_ID_IS_NULL;
        }
        if (code == 9007) {
            return FILE_MUST_BE_LESS_THAN_10M;
        }
        if (code == 9008) {
            return FILE_NOT_EXIST;
        }
        if (code == 9009) {
            return NO_CACHE_DATA;
        }
        if (code == 9010) {
            return TIME_OUT;
        }
        if (code == 9011) {
            return BMOB_USER_DOES_NOT_SUPPORT_BATCH_OPERATION;
        }
        if (code == 9012) {
            return CONTEXT_IS_NULL;
        }
        if (code == 9013) {
            return DATABASE_NAME_FORMAT_IS_NOT_CORRECT;
        }
        if (code == 9014) {
            return AUTHORIZATION_IS_FAILURE;
        }
        if (code == 9016) {
            return NETWORK_IS_NOT_AVAILABLE;
        }
        if (code == 9017) {
            return THIRD_LOGIN_ERROR;
        }
        if (code == 9018) {
            return PARAMETER_IS_NULL;
        }
        if (code == 9019) {
            return FORMAT_IS_NOT_CORRECT;
        }
        if (code == 301) {
            return EMAIL_ADDRESS_IS_VAILD;
        }
        if (code == 10010) {
            return MESSAEG_IS_LIMIT;
        }
        return null;
    }

    public static boolean avaiableMedia() {
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 验证邮箱地址是否正确
     *
     * @param email
     * @return
     */
    public static boolean checkEmail(String email) {
        boolean flag = false;
        try {
            String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }

        return flag;
    }

    /**
     * 验证手机号码
     *
     * @param mobiles
     * @return [0-9]{5,9}
     */
    public static boolean isMobileNO(String mobiles) {
        boolean flag = false;
        try {
            Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
            Matcher m = p.matcher(mobiles);
            flag = m.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    public static boolean isNum(String number) {
        boolean flag = false;
        try {
            Pattern p = Pattern.compile("^[0-9]{5}$");
            Matcher m = p.matcher(number);
            flag = m.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    public boolean isOnline() {
        MyUser user = BmobUser.getCurrentUser(MyUser.class);
        if (user == null) {
            return false;
        }
        return true;
    }
}
